package com.example.oauth.authserver.repository.entity;

import com.google.common.collect.Sets;
import lombok.Data;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.GenericGenerator;

import javax.persistence.*;
import java.time.LocalDateTime;
import java.util.Set;

/**
 * <p>
 *
 * </p>
 *
 * @author chang.zhou
 * @version 1$
 * @date Created in 2022/3/9 0:20
 */
@Getter
@Setter
@Entity
@Table(name = "auth_role")
public class Role {

    @Id
    @GenericGenerator(name = "snowflake", strategy = "com.example.oauth.authserver.common.SnowFlakeGenerator")
    @GeneratedValue(generator = "snowflake")
    private Long id;

    @Column(name = "role_name")
    private String roleName;

    @Column(name = "role_code", unique = true)
    private String roleCode;

    private String comments;

    @Column(name = "create_time")
    private LocalDateTime createTime;

    // 放弃控制权，完全交给多对多的对方维护
    @ManyToMany(mappedBy = "roles")
    private Set<Subject> subjects = Sets.newHashSet();

    @ManyToMany(targetEntity = Resource.class,cascade = CascadeType.ALL, fetch = FetchType.EAGER)
    @JoinTable(name = "auth_role_resource",
            joinColumns = {@JoinColumn(name = "role_id",referencedColumnName = "id")},
            inverseJoinColumns = {@JoinColumn(name = "resource_id",referencedColumnName = "id")})
    private Set<Resource> resources = Sets.newHashSet();

}
